<?php
/**
 * 语言分类
 *
 */
class Library extends MyBase{
	private $librarys = array();
	
	function __construct() {
		parent::__construct('library.id', array('name' => 'name', 'mark' => 'mark'));
	}
		
	/**
	 * 修改语言分类
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('pid' => $post['pid'], 'name' => $post['name']);
		if($id > 0){
			$select['kept'] = " AND `id`!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)) {return lang('already').lang('exist');}

		$data = array(
			'pid'		=> $post['pid'],
			'name'		=> $post['name'],
			'mark'		=> $post['mark'],
			'memo'		=> $post['memo'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$id = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$id = $this->db->insert($this->tbname, $data);
		}
		if($id > 0){
			return $this->cache_library();
		}
		return $id;
	}
			
	/**
	 * 删除语言分类
	 */
	public function del($post){
		if(!is_array($post['list'])) {
			$post['list'] = array($post['list']);
		}
		$iRtn = 0;
		foreach($post['list'] as $id){
			$sublist = $this->db->sql_row("SELECT * from `{$this->tbname}` WHERE pid=$id");
			if(!empty($sublist)){
				continue;
			}
			if($this->db->delete($this->tbname, array('id' => $id))){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_library();
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		$select = array('id' => $id);
		return $this->db->get_row($this->tbname, $select, $fields);
	}*/
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['name'])){
			$select['kept'] .= " AND `name` LIKE '%{$select['name']}%'";
			unset($select['name']);
		}
		if(!empty($select['mark'])){
			$select['kept'] .= " AND `mark` LIKE '%{$select['mark']}%'";
			unset($select['mark']);
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/********************************************************************************
	 * 取菜单树
	 ********************************************************************************/
	
	public function get_library_list($pid = 0, $level = 0, $depth = 0){
		//$where = empty($keyword) ? '' : " AND (name LIKE '%$keyword%' OR mark LIKE '%$keyword%')";
		$list = $this->db->sql_all("SELECT * from `{$this->tbname}` WHERE pid=$pid");
		if(isset($list['list']) && !empty($list['list'])){
			foreach($list['list'] as $row){
				$row['level'] = $level;
				$this->librarys[$row['id']] = $row;
				$next_level = $level + 1;
				if($depth == 0 || $next_level < $depth){
					$this->get_library_list($row['id'], $next_level, $depth);
				}
			}
		}
		return $this->librarys;
	}
	
	public function get_library_tree_ul($pid = 0, $except_id = 0, $level = 99){
		$librarys = array();
		if($pid == 0){
			$librarys[] = '<li><a href="#" onClick="set_library_id(this, 0);return false;">根</a><ul>';
		}
		$list = $this->db->sql_all("SELECT * from `{$this->tbname}` WHERE pid=$pid");
		if(isset($list['list']) && !empty($list['list'])){
			foreach($list['list'] as $row){
				$sub_librarys = $this->get_library_tree_ul($row['id'], $except_id, $level + 1);
				if(!empty($sub_librarys)){
					$librarys[] = '<li><a href="#" onClick="set_library_id(this,'.$row['id'].');return false;">'.$row['name'].'</a>';
					
					$librarys[] = '<ul>';
					$librarys[] = '<li class="topline"></li>';
					$librarys[] = $sub_librarys;
					$librarys[] = '</ul></li>';
				}else{
					$librarys[] = '<li><a href="#" onClick="set_library_id(this,'.$row['id'].');return false;">'.$row['name'].'</a></li>';
				}
				
			}
		}
		
		if($pid == 0){
			$librarys[] = '</ul></li>';
		}
		return implode("\n", $librarys);
	}
	
	public function get_library_tree($pid = 0, $except_id = 0, $level = 0){
		$librarys = array();
		$list = $this->db->sql_all("SELECT * from `{$this->tbname}` WHERE pid=$pid");
		if(isset($list['list']) && !empty($list['list'])){
			foreach($list['list'] as $row){
				$row['level'] = $level;
				$sub_librarys = $this->get_library_tree($row['id'], $except_id, $level + 1);
				if(!empty($sub_librarys)){
					$row['librarys'] = $sub_librarys;
				}
				$librarys[$row['id']] = $row;			
			}
		}
		return $librarys;
	}	
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache_library(){
		$library_list = $this->get_library_list();
		$library_tree = $this->get_library_tree();
		$library_mark_list = array();
		foreach($library_list as $lib){
			$library_mark_list[$lib['mark']] = $lib['id'];
		}
		$r = cache_write('librarys.php', array('library_list' => $library_list, 'library_tree' => $library_tree, 'library_mark_list' => $library_mark_list));
		if($r > 0){
			return 1;
		}else{
			return -1;
		}
	}
}
?>